package controllers;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import model.entities.OutletMasterGlobalTemp;
import myDaos.OutletMasterDao;

@ManagedBean
@ViewScoped
public class OutletMasterManagedBean {
private String distributor;
private String country;
private List<OutletMasterGlobalTemp> omlist;
private boolean isMethodCalled = false;
private List<SelectItem> distList;
private String gui;

@ManagedProperty("#{servicesManagedBean}")
private ServicesManagedBean servicesManagedBean;

@EJB
private OutletMasterDao omd;


public String getGui() {
	return gui;
}

public void setGui(String gui) {
	this.gui = gui;
}

public List<SelectItem> getDistList() {
	return distList;
}

public void setDistList(List<SelectItem> distList) {
	this.distList = distList;
}

public String getCountry() {
	return country;
}

public void setCountry(String country) {
	this.country = country;
}


public ServicesManagedBean getServicesManagedBean() {
	return servicesManagedBean;
}

public void setServicesManagedBean(ServicesManagedBean servicesManagedBean) {
	this.servicesManagedBean = servicesManagedBean;
}

public String getDistributor() {
	return distributor;
}

public void setDistributor(String distributor) {
	this.distributor = distributor;
}

public List<OutletMasterGlobalTemp> getOmlist() {
	return omlist;
}

public void setOmlist(List<OutletMasterGlobalTemp> omlist) {
	this.omlist = omlist;
}

public boolean isMethodCalled() {
	return isMethodCalled;
}

public void setMethodCalled(boolean isMethodCalled) {
	this.isMethodCalled = isMethodCalled;
}

public void callOutletProcedure(){
	Boolean procedureReturn;
	procedureReturn=omd.callOutletProcedure(distributor);
	if(procedureReturn)
	{
		isMethodCalled=true;
		updateList();
	}
}

public void updateList()
{
	omlist=omd.getOutletMasterList(distributor,gui);
	isMethodCalled = true;
}

public void updateDistributorsList()
{
	//Proposed by Samy
	distList = new ArrayList<SelectItem>();
	for(int i = 0; i < servicesManagedBean.getCountriesDistributorList().size(); i++) 
    {  
		if(country!=null && !country.trim().equals("") && 
				  servicesManagedBean.getCountriesDistributorList().get(i).getCountry().equals(country))
		{
		distList.add(new SelectItem(
									 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
									 ,
									 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
				                    )
		             );
		}
    }
}


}
